const { manager } = require('../models/index')
const errorHelper = require('../util/errorHelper')
const tokenGenerator = require('../util/tokenGenerator')

class ManagerController {
  constructor() {
    // empty
  }
  async find(where) {
    const managers = await manager.findAll(where)
    if (!managers.length) {
      return errorHelper(400, 'account or password error')
    }
    return managers
  }
  async login({ managerId, password, loginTime, outdateTime }) {
    const isExist = await manager.findAll({ where: { managerId, password } })
    if (!isExist.length) {
      return errorHelper(400, 'account or password error')
    }
    const mId = isExist[0].managerId
    const loginToken = tokenGenerator.generate(managerId, 10)
    const result = await manager.update({ token: loginToken, loginTime, outdateTime }, { where: { managerId } })
    return { token: loginToken, managerId: mId }
  }

  // async checkToken() {
  // }
}

module.exports = new ManagerController()
